package com.jty.lib_base.widget.pagerphoto;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.os.Bundle;

import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;

import android.widget.ImageView;
import android.widget.MediaController;
import android.widget.TextView;
import android.widget.VideoView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.viewpager.widget.ViewPager;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.target.CustomTarget;
import com.bumptech.glide.request.transition.Transition;
import com.jty.lib_base.R;
import com.lxj.xpopup.photoview.OnOutsidePhotoTapListener;
import com.lxj.xpopup.photoview.PhotoView;


import java.util.ArrayList;
import java.util.List;


/**
 * Created by Administrator on 2017/5/3.
 * 嵌套了viewpager的图片浏览
 */

public class ShowImagesDialog extends Dialog {

    private View mView ;
    private Context mContext;
    private PhotoView mPhotoView;
    private VideoView mVideoView;
    private ImageView mIvExit;
    private String mUrl;

    public ShowImagesDialog(@NonNull Context context) {
        super(context, R.style.transparentBgDialog);
        this.mContext = context;
        initView();
    }

    private void initView() {
        mView = View.inflate(mContext, R.layout.dialog_images_brower, null);
        mPhotoView = (PhotoView) mView.findViewById(R.id.pv_img);
        mVideoView = (VideoView) mView.findViewById(R.id.videoView);
        mIvExit = (ImageView) mView.findViewById(R.id.iv_exit);
        mPhotoView.setOnOutsidePhotoTapListener(new OnOutsidePhotoTapListener() {
            @Override
            public void onOutsidePhotoTap(ImageView imageView) {
                dismiss();
            }
        });
        mIvExit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dismiss();
            }
        });

        this.setOnDismissListener(new OnDismissListener() {
            @Override
            public void onDismiss(DialogInterface dialogInterface) {
                if(mVideoView.isPlaying()) {
                    mVideoView.pause();
                }
            }
        });
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        int uiOptions = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                | View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                | View.SYSTEM_UI_FLAG_IMMERSIVE
                | View.SYSTEM_UI_FLAG_FULLSCREEN;
        this.getWindow().getDecorView().setSystemUiVisibility(uiOptions);

        setContentView(mView);
        Window window = getWindow();
        WindowManager.LayoutParams wl = window.getAttributes();
        wl.x = 0;
        wl.y = 0;
        DisplayMetrics metrics = new DisplayMetrics();
        ((Activity)mContext).getWindowManager().getDefaultDisplay().getMetrics(metrics);
        wl.height = metrics.heightPixels;
        wl.width = metrics.widthPixels;
        wl.gravity = Gravity.CENTER;
        window.setAttributes(wl);
    }



    public void setmImgUrl(String url, boolean isVideo) {
        mUrl = url;

        if(isVideo) {
            mPhotoView.setVisibility(View.GONE);
            mVideoView.setVisibility(View.VISIBLE);
            MediaController  localMediaController = new MediaController(mContext);
            localMediaController.setVisibility(View.INVISIBLE);
            mVideoView.setMediaController(localMediaController);
            mVideoView.setVideoPath(url);
            mVideoView.start();
        } else {
            mPhotoView.setVisibility(View.VISIBLE);
            mVideoView.setVisibility(View.GONE);
            Glide.with(mContext).asBitmap().load(url).skipMemoryCache(true).diskCacheStrategy(DiskCacheStrategy.NONE)
                    .placeholder(R.drawable.ic_avatar_black)
                    .error(R.drawable.ic_avatar_black)
                    .into(new CustomTarget<Bitmap>() {
                        @Override
                        public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition<? super Bitmap> transition) {
                            mPhotoView.setImageBitmap(resource);
                        }

                        @Override
                        public void onLoadFailed(@Nullable Drawable errorDrawable) {
                            super.onLoadFailed(errorDrawable);
                        }

                        @Override
                        public void onLoadCleared(@Nullable Drawable placeholder) {

                        }
                    });
        }
    }

}
